package 剑指offer;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by lenovo】 on 2017/3/30.\
 * 把只包含因子2、3和5的数称作丑数（Ugly Number）。
 * 例如6、8都是丑数，但14不是，因为它包含因子7。
 * 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。
 */
public class GetUglyNumber {
    public int GetUglyNumber_Solution(int index) {
        if (index == 0)
            return 0;
        int maxIndexOfUgly = index - 1;
        int i,i2,i3,i5;
        i = i2 = i3 = i5 = 0;
        int [] ugly = new int[index];
        ugly[i2] = 1;

        while( i < maxIndexOfUgly) {
            ugly[++i] = min(2 * ugly[i2] , 3 * ugly[i3], 5 * ugly[i5]);
            if (ugly[i] == 2 * ugly[i2])
                i2 ++;
            if (ugly[i] == 3 * ugly[i3])
                i3 ++;
            if (ugly[i] == 5 * ugly[i5])
                i5 ++;
        }
        System.out.println(ugly[index-1]);
        return ugly[index - 1];
    }

    public int min(int first, int second, int third) {

        int temp = first < second ? first : second;

        return temp < third ? temp : third;
    }
}
